package com.itheima.service.impl;


import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.CheckItemDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author qzh
 * @create 2020-01-06 2020/1/6
 */
@Service(interfaceClass = CheckItemService.class)
@Transactional
public class CheckItemServiceImpl implements CheckItemService {


    @Autowired
    private CheckItemDao checkItemDao;

    /**
     * 增加检查项
     * @param checkItem
     */
    public void add(CheckItem checkItem){

        checkItemDao.add(checkItem);

    }

    /**
     * 分页查询
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {

        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();

        PageHelper.startPage(currentPage,pageSize);
        Page<CheckItem> page = checkItemDao.selectByCondition(queryPageBean);

        long total = page.getTotal();
        List<CheckItem> rows = page.getResult();


        return new PageResult(total,rows);
    }

    /**
     * 根据id删除检查项
     * @param id
     */
    @Override
    public void deleteById(Integer id) {
        //判断当前的检查项是否关联检查组
        long count = checkItemDao.findCountByCheckItemId(id);
        if(count>0){
            //不允许删除
            new RuntimeException();
        }


        checkItemDao.deleteById(id );


    }

    @Override
    public void edit(CheckItem checkItem) {

        checkItemDao.edit(checkItem);

    }

    @Override
    public CheckItem findById(Integer id) {

        return checkItemDao.findById(id);
    }

    @Override
    public List<CheckItem> findAll() {

        return checkItemDao.findAll();


    }


}
